/* ca_test.c
 *
 * Simple test app that will no doubt grow and grow :-)
 */

/*
 * Licensed to the ConArtist Development Team (CADT).
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 * The CADT licenses this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include <apr_file_io.h>

#include "conartist.h"

#define CK_HOST   "localhost"
#define CK_PORT   8080

static void makeRequest(const char *uri, apr_pool_t *pool)
{
    struct caContent ct;
    apr_status_t rv;
    apr_time_t now;

    memset(&ct, 0, sizeof(ct));
    ct.hostname = CK_HOST;
    ct.port = CK_PORT;
    ct.uri = uri;
    ct.pool = pool;

    printf("Requesting %s\n", uri);
    now = apr_time_now();
    rv = caGetContent(&ct);
    now = apr_time_now() - now;

    if (rv == APR_SUCCESS) {
        printf("\t%s OK\n", ct.uri);
        printf("\tfilename = '%s'\n", ct.filename);
    } else
        printf("\tFailed to get %s\n", ct.uri);
    printf("Request took %ld usecs\n", now);
}

int main(int argc, char **argv)
{
    apr_status_t rv;
    char *fn = NULL;
    char *buffer;
    apr_size_t bufSz = 0;
    apr_pool_t *testPool = NULL;

    printf("ConArtist Test Application\n==========================\n\n");

    apr_initialize();
    rv = apr_pool_create(&testPool, NULL);
    if (rv != APR_SUCCESS) {
        printf("Unable to allocate a test pool!\n");
        exit(1);
    }
    atexit(apr_terminate);

    rv = caInit(testPool);
    if (rv != APR_SUCCESS) {
        printf("failed to initialise library!\n");
        exit(1);
    }
    printf("Initialised OK\n");
    printf("Version: %s\n", caGetVersion());

    if (argc > 1) {
        printf("Attempting to parse config file '%s'\n", argv[1]);
        rv = caConfigFileParse(argv[1]);
    } else 
        rv = caConfigFileParse("testConfig.xml");
    if (rv != APR_SUCCESS) {
        printf("Error parsing configuration file!\n");
        exit(1);
    }

    makeRequest("/index.html", testPool);
    makeRequest("/index.html", testPool);
    makeRequest("/index.html", testPool);
    makeRequest("/doap/ant.html", testPool);
    makeRequest("/doap/portable_runtime.html", testPool);
    makeRequest("/generated/filelist.xml", testPool);

    return 0;
}
